//*************************************************
//Program #4       FIFO.cpp
//*************************************************
//Names: Stephen Durfrey,  Andrew Brock
//Due: April 23, 2010
//Operating Systems
//Related Files:  FIFO.h
// FIFO PRA: Simplest pra, associates with each page the time
//  when that page was brought to memory.

#include "FIFO.h"
#include <vector>
#include <string>
#include <stdlib.h>
#include <algorithm>

using namespace std;

//Default constructor
FIFO::FIFO()
{
}

//*************************************************
//FIFO::init - initializes the varibles based on the variables
// passed into the program.
//*************************************************
void FIFO::init(int frames, vector<int> ref_str)
{
	num_frames = frames;
	ref_string = ref_str;
	num_faults = 0;
	ref_page = ref_string.begin();
}
//*************************************************
//FIFO::checkFrame - checks to see if the value "check"
// is in the frames.  If it is its location is passed back
// otherwise check == frames.end()
//*************************************************
vector<int>::iterator FIFO::checkFrame(vector<int>::iterator check)
{
	bool found = false;
	vector<int>::iterator value, begin, end;

	value = end = frames.end();

	for(begin = frames.begin(); begin != end && !found; begin++)
	{
		if(*begin == *check)
		{
			value = begin;
			found = true;
		}
	}

	return value;

}
//*************************************************
//FIFO::removePage - this is where the loop is that the FIFO
// page replacement algorithm is tested.
//*************************************************
void FIFO::removePage()
{
	
	vector<int>::iterator begin, end, pos, position;
	end = ref_string.end();
	ref_page = ref_string.begin();

	do 
	{//BEGIN DO-WHILE
		if(frames.size() != num_frames)
		{//BEGIN IF
			//First checks to see if ref_page is already in the frames
			position = checkFrame(ref_page);
			if(position == frames.end())
			{//Frames has an empty slot
				frames.push_back(*ref_page);
				num_faults++;
				framesVictim = frames.begin();
			}
		}//END IF
		else
		{//BEGIN ELSE
			
			//check to see if the ref_page currently exists in the set of frames
			pos = find(frames.begin(), frames.end(), *ref_page);

			//replaces the frame thats in line to be the next victim.
			if(pos == frames.end())
			{
				*framesVictim = *ref_page;
				num_faults++;

				//if framesVictim is out of victims reset to start, else move to
				//  next victim.
				if((framesVictim + 1) == frames.end())
				{
					framesVictim = frames.begin();
				}
				else
				{
					framesVictim++;
				}
			}
		}//END ELSE
		ref_page++;
	}while(ref_page != end);//END DO-WHILE
}
//*************************************************
//FIFO::run - executes the algorithm and then outputs
// the results of values num_frames and num_faults
//*************************************************
void FIFO::run()
{
	char value[50];
	string value_param;
	removePage();

	sprintf(value, "%d , %d", num_frames, num_faults);

	value_param = (string)value;
	addCsvString("FIFO.txt", value_param );
}

